package com.example.bankend.util;

/**
 * @author luolinila
 * @date 2025/6/17 15:42
 */
public class Validator {

    private static final String phoneRegex = "^1[3-9]\\d{9}$";

    public static boolean isValidPhoneNumber(String phoneNumber) {
        return phoneNumber.matches(phoneRegex);
    }

    // 定义身份证号码的正则表达式
    private static final String ID_CARD_REGEX = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";

    // 定义地区码表
    private static final String[] areaCode = {
        "11", "12", "13", "14", "15", "21", "22", "23", "31", "32",
        "33", "34", "35", "36", "37", "41", "42", "43", "44", "45",
        "46", "50", "51", "52", "53", "54", "61", "62", "63", "64",
        "65", "71", "81", "82", "91"
    };

    // 校验身份证号码
    public static boolean isValidIDCard(String idCard) {
        // 判断是否为空
        if (idCard == null || idCard.isEmpty()) {
            return false;
        }

        // 判断长度是否为 15 位或 18 位
        if (idCard.length() != 15 && idCard.length() != 18) {
            return false;
        }

        // 判断是否符合正则表达式
        if (!idCard.matches(ID_CARD_REGEX)) {
            return false;
        }

        // 判断地区码是否有效
        String code = idCard.substring(0, 2);
        boolean isAreaCodeValid = false;
        for (String area : areaCode) {
            if (area.equals(code)) {
                isAreaCodeValid = true;
                break;
            }
        }
        if (!isAreaCodeValid) {
            return false;
        }

        // 判断出生日期是否有效（对于 18 位身份证）
        if (idCard.length() == 18) {
            String birthDate = idCard.substring(6, 14);
            boolean isBirthDateValid = isValidBirthDate(birthDate);
            if (!isBirthDateValid) {
                return false;
            }

            // 校验 18 位身份证的校验码
            if (!isValidCheckCode(idCard)) {
                return false;
            }
        }

        // 判断 15 位身份证的出生日期是否有效
        else if (idCard.length() == 15) {
            String birthDate = idCard.substring(6, 12);
            birthDate = "19" + birthDate; // 补充年份前缀
            boolean isBirthDateValid = isValidBirthDate(birthDate);
            if (!isBirthDateValid) {
                return false;
            }
        }

        return true;
    }

    // 校验出生日期是否有效
    private static boolean isValidBirthDate(String birthDate) {
        String year = birthDate.substring(0, 4);
        String month = birthDate.substring(4, 6);
        String day = birthDate.substring(6, 8);

        // 判断年份是否在合理范围内（这里假设 1900-2099 是合理范围）
        int yearInt = Integer.parseInt(year);
        if (yearInt < 1900 || yearInt > 2099) {
            return false;
        }

        // 判断月份是否有效
        int monthInt = Integer.parseInt(month);
        if (monthInt < 1 || monthInt > 12) {
            return false;
        }

        // 判断日期是否有效
        int dayInt = Integer.parseInt(day);
        int maxDay;

        switch (monthInt) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                maxDay = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                maxDay = 30;
                break;
            case 2:
                if ((yearInt % 4 == 0 && yearInt % 100 != 0) || yearInt % 400 == 0) {
                    maxDay = 29; // 闰年
                } else {
                    maxDay = 28; // 平年
                }
                break;
            default:
                return false;
        }

        if (dayInt < 1 || dayInt > maxDay) {
            return false;
        }

        return true;
    }

    // 校验 18 位身份证的校验码
    private static boolean isValidCheckCode(String idCard) {
        // 定义加权因子和校验码表
        int[] weightFactor = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] checkCodeTable = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

        // 计算校验码
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += Integer.parseInt(idCard.charAt(i) + "") * weightFactor[i];
        }
        int remainder = sum % 11;
        char expectedCheckCode = checkCodeTable[remainder];

        // 判断校验码是否正确
        char actualCheckCode = idCard.charAt(17);
        return expectedCheckCode == actualCheckCode || (expectedCheckCode == 'X' && actualCheckCode == 'x');
    }

    public static boolean isValidPassword(String password) {
        // 判断是否为空
        if (password == null || password.isEmpty()) {
            return false;
        }

        // 判断长度是否在 8 到 16 位之间
        if (password.length() < 8 || password.length() > 16) {
            return false;
        }

        // 定义正则表达式，只允许数字和大小写字母
        String regex = "^[a-zA-Z0-9]+$";
        return password.matches(regex);
    }
    public static boolean is16DigitNumber(String str) {
        return str.matches("^\\d{16}$");
    }

    public static boolean isCardPass(String str) {
        return str.matches("^\\d{6}$");
    }
}
